define(['Util', 'jquery', './getPath', 'cookie'], function(Util ,$ , AjaxPath, $cookie){
	var getProjectList = function(userId, call, changeCallBack) {
		Util.ajax.getJson( AjaxPath.project + "/projects?userId=" + userId, {}, function(result, isok){
			var str = "",
				dlWidth = null,
				dlLength = null,
				isFlag = false;
				$(result.beans).each(function(index, item) {
					item.projectName = $("<span/>").text(item.projectName).html();
					item.projectName = item.projectName.replace(/\s/g, '&nbsp;');
					if(parseInt($cookie.get("projectSelectId"))===item.id){
						isFlag = true;
						str +=  '<dl data="'+ item.id +'" title="'+ item.projectName +'" class="pro_active">'+
								'<dt></dt>'+
								'<dd>'+ item.projectName +'</dd>'+
							'</dl>';
					}else {
						str +=  '<dl data="'+ item.id +'" title="'+ item.projectName +'">'+
								'<dt></dt>'+
								'<dd>'+ item.projectName +'</dd>'+
							'</dl>';
					}
					
				})

			$(".t-project-list-content").html(str);
			if(!$cookie.get("projectSelectId")){
				$(".t-project-list-content dl:first").addClass("pro_active");
				$cookie.set("projectSelectId", $(".t-project-list-content dl:first").attr("data"), 86400000);
			}
			dlWidth = $(".t-project-list-content dl").width() + 50;
			dlLength = $(".t-project-list-content:first dl").length;
			$(".t-project-list-content").width(dlWidth * dlLength);

			if(dlWidth * dlLength > $(".t-project-list-container").width()) {
				$(".t-project-list-container-tip").show();
				$(".t-project-list-container").hover(function() {
					$(".dragscroll").css("cursor", "ew-resize");
				}, function() {
					$(".dragscroll").css("cursor", "default");
				})
				$(".t-project-list-container").scroll(function() {
					var scrollLeft = $(".t-project-list-container").scrollLeft();
					$(".t-project-list-container-tip").hide();
					if(scrollLeft==0){
						$(".t-project-list-btn-prev").addClass('hide');
						// $(".t-project-list-btn-next").removeClass('hide');
					}else if(scrollLeft>0){
						$(".t-project-list-btn-prev").removeClass('hide');
					}
					if(scrollLeft == dlWidth * dlLength - $(".t-project-list-container").width()){
						$(".t-project-list-btn-next").addClass('hide');
					}else{
						$(".t-project-list-btn-next").removeClass('hide');
					}
				})
			}

			// $(".t-project-list-btn-prev").click(function(){
			// 	// var scrollLeftWdith = 0;
			// 	var scrollLeftWdith = $(this).siblings(".t-project-list-content").css("left");
			// 	scrollLeftWdith = parseInt(scrollLeftWdith);
			// 	if(scrollLeftWdith>=0){
			// 		return;
			// 	}
			// 	$(this).siblings(".t-project-list-content").not(":animated").animate({"left":scrollLeftWdith + dlWidth },300);

			// })
			// $(".t-project-list-btn-next").click(function(){
			// 	var scrollRightWdith = $(this).siblings(".t-project-list-content").css("left");
			// 	scrollRightWdith = parseInt(scrollRightWdith);
			// 	var containerWidth = $(this).parent(".t-project-list-container").width();

			// 	if((dlWidth * dlLength - containerWidth) <= Math.abs(scrollRightWdith)){
			// 		return;
			// 	}
			// 	$(this).siblings(".t-project-list-content").not(":animated").animate({"left":scrollRightWdith - dlWidth },300);
			// })

			$(".t-project-list-content dl").mousedown(function() {
				$cookie.set("projectSelectId", $(this).attr("data"), 86400000);
				$(this).addClass("pro_active").siblings("dl").removeClass("pro_active");
				if(changeCallBack){
					changeCallBack();
				}
			})

			if(result.beans.length===0){
				$(".t-project-list-content").css("width","100%");
				$(".t-project-list-content").html('<div class="fn-center mt-30">您当前没有任何项目,请联系系统管理员!</div>');
				$cookie.set("projectSelectId",-1);
			}
			if( isFlag === false && result.beans.length>0){
				$cookie.set("projectSelectId", result.beans[0].id, 86400000);
				$(".t-project-list-content").each(function(index,item){
					$(this).children("dl:first").addClass("pro_active");
				})
			}
			if(call){
				call();
			}

			var addEventListener = 'addEventListener';
			var elems = $(".dragscroll");
			for (var i = 0; i < elems.length;) {
			    (function(elem, lastClientX, lastClientY, pushed) {
			    	elem[addEventListener]('mouseover', function(e) {
			    		$("body").css("backgroud","#000");
			    	}, 0);

			        elem[addEventListener]('mousedown', function(e) {
			        	$(".t-project-list-container-tip").hide();
			            pushed = 1;
			            lastClientX = e.clientX;
			            lastClientY = e.clientY;
			            e.preventDefault();
			            e.stopPropagation();
			        }, 0);
			        
			        window[addEventListener]('mousemove', function(e) {
			            if (pushed) {
			                elem.scrollLeft -=
			                    (- lastClientX + (lastClientX=e.clientX));
			                elem.scrollTop -=
			                    (- lastClientY + (lastClientY=e.clientY));
			            }
			        }, 0);
			         
			        window[addEventListener]('mouseup', function(){
			            pushed = 0;
			        }, 0);

			     })(elems[i++]);
			}

			$(".dragscroll, .t-project-list-content dl").mousedown(function(e) {
				e.preventDefault();
				e.stopPropagation();
			})
			// console.log($(".t-project-list-content").width(),$(".t-project-list-container").width());
			if( parseInt( $(".t-project-list-content").width() ) < parseInt( $(".t-project-list-container").width() ) ){
				$(".t-project-list a.t-project-list-btn.t-project-list-btn-next").hide();
			};
		})
	}

	return getProjectList;
})
